class TerrainLayer {
	constructor(viewer) {
		this._viewer = viewer;
		this._scene = viewer.scene;
		this._currentTerrain;
	}

	/**
	 * @description 地形夸张倍数
	 * @param {Number} terrainExaggeration
	 */
	set terrainExaggeration(terrainExaggeration) {
		this._terrainExaggeration = terrainExaggeration;
		this._viewer.terrainExaggeration = this._terrainExaggeration;
	}
	get terrainExaggeration() {
		return this._terrainExaggeration;
	}

	add(options, complete) {
		let id = Cesium.defaultValue(options.id, Cesium.createGuid())
		let layer = this.create(options);
		if (Cesium.defined(layer)) {
			this._currentTerrain = layer;
			this.terrainProvider = this._currentTerrain;
			if (complete) {
				complete({
					code: 200,
					message: '成功.',
					data: {
						id: id,
						layer: this._currentTerrain
					}
				});
			}
			return this._currentTerrain;
		}

		if (complete) {
			complete({
				code: 500,
				message: '失败.'
			});
		}
	}

	create(options) {
		let terrain = Cesium.defaultValue(options, {});
		//无地形
		if ('none' == terrain.type) {
			return new Cesium.EllipsoidTerrainProvider({
				ellipsoid: Cesium.Ellipsoid.WGS84
			});
		}
		//标准xyz服务
		else if ('xyz' == terrain.type) {
			return new Cesium.CesiumTerrainProvider({
				url: Cesium.defaultValue(terrain.url, ""),
			})
		}
		//ArcGIS地形服务
		if ('arcgis' == terrain.type) {
			let param = {
				url: Cesium.defaultValue(terrain.url,
					"https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"
					),
			};
			if (Cesium.defined(terrain.token)) {
				param.token = terrain.token
			}
			if (Cesium.defined(terrain.ellipsoid)) {
				param.ellipsoid = Cesium.defaultValue(terrain.ellipsoid, Cesium.Ellipsoid.WGS84)
			}
			return new Cesium.ArcGISTiledElevationTerrainProvider(param)
		}
		//cesium官方ion在线服务
		else if ('ion' == terrain.type) {
			return Cesium.createWorldTerrain({
				requestVertexNormals: Cesium.defaultValue(terrain.requestVertexNormals, false),
				requestWaterMask: Cesium.defaultValue(terrain.requestWaterMask, false),
				requestMetadata: Cesium.defaultValue(terrain.requestMetadata, true),
				ellipsoid: Cesium.defaultValue(terrain.ellipsoid, Cesium.Ellipsoid.WGS84),
				credit: Cesium.defaultValue(terrain.credit, "")
			})
		}
		//谷歌地球企业服务
		else if ('gee' == terrain.type) {
			if (Cesium.defined(terrain.proxy)) {
				return new Cesium.GoogleEarthEnterpriseTerrainProvider({
					metadata: new Cesium.GoogleEarthEnterpriseMetadata(new Cesium.Resource({
						url: Cesium.defaultValue(terrain.url,
							'http://www.earthenterprise.org/3d'),
						proxy: new Cesium.DefaultProxy(Cesium.defaultValue(terrain.proxy, '')),
					})),
					ellipsoid: Cesium.defaultValue(terrain.ellipsoid, Cesium.Ellipsoid.WGS84),
					credit: Cesium.defaultValue(terrain.credit, "")
				});
			}
			return new Cesium.GoogleEarthEnterpriseTerrainProvider({
				metadata: new Cesium.GoogleEarthEnterpriseMetadata(Cesium.defaultValue(terrain.url,
					'http://www.earthenterprise.org/3d')),
				ellipsoid: Cesium.defaultValue(terrain.ellipsoid, Cesium.Ellipsoid.WGS84),
				credit: Cesium.defaultValue(terrain.credit, "")
			});
		}
		//vr地形服务
		else if ('vr' == terrain.type) {
			return new Cesium.VRTheWorldTerrainProvider({
				url: Cesium.defaultValue(terrain.url,
					"https://www.vr-theworld.com/vr-theworld/tiles1.0.0/73/"),
				ellipsoid: Cesium.defaultValue(terrain.ellipsoid, Cesium.Ellipsoid.WGS84),
				credit: Cesium.defaultValue(terrain.credit, "")
			});
		} else {
			console.error("未知类型，默认显示!");
			return new Cesium.EllipsoidTerrainProvider({});
		}
	}
	/**
	 * @description 移出
	 */
	remove() {
		this._currentTerrain = new Cesium.EllipsoidTerrainProvider({})
		this.terrainProvider = this._currentTerrain;
	}

	/**
	 * @description 销毁
	 */
	destroy() {
		delete this._scene
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}
export {
	TerrainLayer
}
